/**
 * js文件
 * 
 * @author: zhongXu
 * @date : 2017-6-7
 */

$(document).ready(
				function() {
					$("form").submit(function() {
						return false;
					});
                    var YNBean={
                        Value : ['Y','N'],
                        Text:{
                            Y : 'Y:YES',
                            N : 'N:NO'
                        }
                    };
					var VAL = {
						T_FDPAPLYDN : "FDPAPLYDN",
						T_FBPBISCUS : "FBPBISCUS",
						T_FBPMDLDEF : "FBPMDLDEF",
						EVT_USER :$("#userId").text(),
						NORMAL : "0000000",
						DISABLED_ATTR : {
							"disabled" : true
						},
						ENABLED_ATTR : {
							"disabled" : false
						}
					};

					ACTION_FLG1 = 'N' ;

					var domObj = {
						$evtSeqId:$("#evtSeqId"),
						$dnno : $("#dnno"),
						$cusIdFk : $("#cusIdFk"),
						$mdlIdFk : $("#mdlIdFk"),
						//$plnShipQty : $("#plnShipQty"),
						$shipAddress : $("#shipAddress"),
						$plnShipTimestamp : $("#plnShipTimestamp"),
						$shipNote : $("#shipNote"),
                        $fifoFlg: $("#fifoFlg"),
						buttons : {
							$query_btn :  $("#query_btn"),
							$del_btn :    $("#del_btn"),
							$add_btn :    $("#add_btn"),
							$save_btn :   $("#save_btn"),
							$update_btn : $("#update_btn"),
							$clear_btn :  $("#clear_btn"),
							$close_btn :  $("#close_btn"),
							$open_btn :   $("#open_btn"),
							$add_mdl_btn : $("#add_mdl_btn"),
							$update_mdl_btn : $("#update_mdl_btn"),
							$del_mdl_btn : $("#del_mdl_btn"),
						},
						grids : {
							$delOrderGrd : $("#delOrderGrd"),
							$mdlDefGrd : $("#mdlDefGrd")
						},

                        dialogs : {
							$mdlEditDialog : $("#mdlEditDialog"),
							$mdlEditDialogForm : $("#mdlEditDialogForm"),
							$mdlEditDialog_sel : $("#mdlEditDialog_sel"),
							$plnShipQty : $("#plnShipQty"),
							$mdlEditDialog_cancelBtn : $("#mdlEditDialog_cancelBtn"),
							$mdlEditDialog_saveBtn : $("#mdlEditDialog_saveBtn")
                        }
					};
					
					var baseFnc ={
							convertTimestamp2Date : function(timestamp) {
								var date = new Date(timestamp.substring(0, 10));
								date.setHours(timestamp.substring(11, 13));
								date.setMinutes(timestamp.substring(14, 16));
								date.setSeconds(timestamp.substring(17, 19));
								return date;
							}
					};
					var buttonFnc = {
						queryFnc : function() {
							var dnno, cusIdFk, mdlIdFk, plnShipTimestamp, iary, inObj, outObj;
							dnno = domObj.$dnno.val();// 交货订单号
							cusIdFk = domObj.$cusIdFk.val();// 客户代码
                          var time=  $("#plnShipTimestamp input").val();
                          if(time!=""){
                              plnShipTimestamp =  domObj.$plnShipTimestamp.data(
                                  "datetimepicker").getLocalDate().format(
                                  'yyyy-MM-dd');
						      }

							//mdlIdFk = domObj.$mdlIdFk.val();//产品代码
							iary = {
								dn_no : dnno,
								cus_id_fk : cusIdFk,
								//mdl_id_fk : mdlIdFk
								// pln_ship_timestamp : plnShipTimestamp
							};
							inObj = {
								trx_id : VAL.T_FDPAPLYDN,
								action_flg : "Q",
								iary : [ iary ]
							};
							outObj = comTrxSubSendPostJson(inObj);
							if (outObj.rtn_code === VAL.NORMAL) {
								setGridInfo(outObj.oary, "#delOrderGrd");
							}
						},

						addFnc : function(){
							//清空控件
							domObj.$evtSeqId.val("");
							domObj.$dnno.val("");
							SelectDom.setSelect(domObj.$cusIdFk);
							SelectDom.setSelect(domObj.$mdlIdFk);
							//domObj.$plnShipQty.val("");
							domObj.$shipAddress.val("");
							$("#plnShipTimestamp input").val("");
							domObj.$shipNote.val("");
                            $("#mdlDefGrd").jqGrid('clearGridData');
							//初始化下拉框
							init();
							enableFn();
							domObj.$dnno.attr(VAL.DISABLED_ATTR);
							$("#update_btn").attr(VAL.DISABLED_ATTR);
                            ACTION_FLG1 = "A";
						},

						updateFnc : function () {
							var selRowId;
							selRowId = domObj.grids.$delOrderGrd.jqGrid("getGridParam",
							"selrow");
							if (!selRowId) {
								showErrorDialog("", "请选择需要修改的订单");
								return false;
							}
							var rowData = domObj.grids.$delOrderGrd.jqGrid(
									"getRowData", selRowId);
							if (rowData.act_ship_qty == 0) {
								enableFn();
							}else{
								domObj.$plnShipQty.attr(VAL.ENABLED_ATTR);
							}
							//omObj.$evtSeqId.attr(VAL.DISABLED_ATTR);
							domObj.$dnno.attr(VAL.DISABLED_ATTR);
                            ACTION_FLG1 = "U";
						},
						
						saveFnc : function(){
                            var rowDataList = $("#mdlDefGrd").getRowData();
                            if(rowDataList.length == 0){
                            	showErrorDialog("","保存前请先添加产品代码！");
                            	return false;
							}

							var actionFlg, inObj, outObj, iary, newRowId,rowIds,selRowId;
							var cusIdFk,mdlIdFk,plnShipQty,shipAddress,shipNote,fifoFlg;
							var plnShipTimestamp; 
							//可编辑说明为新增，不可编辑说明为修改
						/*	actionFlg = domObj.$evtSeqId.attr("disabled") === "disabled"
								? "U"
								: "A";*/
							actionFlg = ACTION_FLG1;
							//actionFlg = domObj.$dnno.val() === "" ? "A":"U";
							var dn_no;
							if(actionFlg == "U"){
                                var selRowId = domObj.grids.$delOrderGrd.jqGrid("getGridParam",
                                    "selrow");
                               var  rowData = domObj.grids.$delOrderGrd.jqGrid("getRowData",selRowId);
                                dn_no  = rowData.dn_no;
							}

                            var iaryList = [];
                            for(var i = 0; i < rowDataList.length ; i++){
                                var rowData = rowDataList[i];
                                var mdlIdFk = rowData.mdl_id_fk;
                                var plnShipQty = rowData.pln_ship_qty;

                                cusIdFk = domObj.$cusIdFk.val();
                                shipAddress=domObj.$shipAddress.val();
                                shipNote=domObj.$shipNote.val();
                                fifoFlg=domObj.$fifoFlg.val();
                                plnShipTimestamp = domObj.$plnShipTimestamp.data(
                                    "datetimepicker").getLocalDate().format(
                                    'yyyy-MM-dd');

                                if (!cusIdFk) {
                                    showErrorDialog("", "客户代码不能为空,请选择!");
                                    return false;
                                }

                                if (!mdlIdFk) {
                                    showErrorDialog("", "产品代码不能为空,请选择!");
                                    return false;
                                }
                                if (!plnShipQty) {
                                    showErrorDialog("", "交货数量不能为空,请选择!");
                                    return false;
                                }
                                if (!shipAddress) {
                                    showErrorDialog("", "交货地址不能为空,请输入!");
                                    return false;
                                }
                                if (!shipNote) {
                                    showErrorDialog("", "交货备注不能为空,请输入!");
                                    return false;
                                }
                                if (!plnShipTimestamp) {
                                    showErrorDialog("", "计划交货日期不能为空,请选择计划交货日期!");
                                    return false;
                                }
                                if(!fifoFlg){
                                	showErrorDialog("" ,"先进先出规则不能为空！");
                                	return false;
								}

                                iary = {
                                    dn_no: dn_no,
                                    cus_id_fk: cusIdFk,
                                    mdl_id_fk: mdlIdFk,
                                    pln_ship_qty: plnShipQty,
                                    ship_address: shipAddress,
                                    ship_note: shipNote,
                                    pln_ship_timestamp: plnShipTimestamp,
                                    fifo_flg   : fifoFlg
                                }
                                iaryList.push(iary);
                            }

								//输给后台的数据
								inObj = {
										trx_id :  VAL.T_FDPAPLYDN,
										action_flg : actionFlg,
								    	evt_usr    : VAL.EVT_USER ,
										iary : iaryList
									};
								outObj = comTrxSubSendPostJson(inObj);
								if (outObj.rtn_code === VAL.NORMAL) {
									if(actionFlg == "A"){
											//comfunction有
											for(var i = 0 ; i < outObj.oary.length; i++){
                                                newRowId = getGridNewRowID("#delOrderGrd");
                                                domObj.grids.$delOrderGrd.jqGrid("addRowData", newRowId,
                                                    outObj.oary[i]);
											}
											showSuccessDialog("交货订单新建成功");
											$("#update_btn").attr(VAL.ENABLED_ATTR);
									}else if (actionFlg == "U"){
 								     	selRowId = domObj.grids.$delOrderGrd.jqGrid("getGridParam","selrow");
										domObj.grids.$delOrderGrd.jqGrid("setRowData",selRowId,outObj.oary[0]);
										showSuccessDialog("交货订单信息更新成功");
										unableFn();
                                        ACTION_FLG1 = 'N';
									}
								}
						},
						delFnc : function(){
							var selRowId, rowData, iary, inObj, outObj;
							//获得选中的行ID
							selRowId = domObj.grids.$delOrderGrd.jqGrid("getGridParam",
									"selrow");
							rowData = domObj.grids.$delOrderGrd.jqGrid(
									"getRowData", selRowId);
							if (!selRowId) {
								showErrorDialog("", "请选择需要删除的交货订单");
								return false;
							}
							if (rowData.act_ship_qty>0) {
								showErrorDialog("", "该订单已出货，不能删除");
								return false;
							}
							//删除按钮绑定删除事件
							$("#del_btn").showCallBackWarnningDialog({
								errMsg : "是否删除该订单信息,请确认!!!!",
								callbackFn : function(data) {
									if (data.result == true) {
										//获得选中行的数据
										rowData = domObj.grids.$delOrderGrd.jqGrid(
												"getRowData", selRowId);
										iary = {
											dn_no : rowData.dn_no
										};
										inObj = {
											trx_id : VAL.T_FDPAPLYDN,
											action_flg : "D",
											iary : [iary]
										};
										outObj = comTrxSubSendPostJson(inObj);
										if (outObj.rtn_code === VAL.NORMAL) {
											showSuccessDialog("删除订单成功");
											domObj.grids.$delOrderGrd.jqGrid(
													"delRowData", selRowId);
                                            $("#mdlDefGrd").jqGrid('clearGridData');
										}
									}
								}
							});
						},
						closeFnc : function(){
							var selRowId, rowData, iary, inObj, outObj;
							//获得选中的行ID
							selRowId = domObj.grids.$delOrderGrd.jqGrid("getGridParam",
									"selrow");
							rowData = domObj.grids.$delOrderGrd.jqGrid(
									"getRowData", selRowId);
							if (!selRowId) {
								showErrorDialog("", "请选择需要关闭的订单");
								return false;
							}
							if (rowData.dn_stat=='CLOSE') {
								showErrorDialog("", "改定单已经是CLOSE状态");
								return false;
							}
							//删除按钮绑定删除事件
							$("#close_btn").showCallBackWarnningDialog({
								errMsg : "是否关闭该订单,请确认!!!!",
								callbackFn : function(data) {
									if (data.result == true) {
										//获得选中行的数据
										rowData = domObj.grids.$delOrderGrd.jqGrid(
												"getRowData", selRowId);
										iary = {
                                            dn_no: rowData.dn_no,
										};
										inObj = {
											trx_id : VAL.T_FDPAPLYDN,
											action_flg : "C",
											iary : [iary]
										};
										outObj = comTrxSubSendPostJson(inObj);
										if (outObj.rtn_code === VAL.NORMAL) {
											showSuccessDialog("关闭订单成功");
										    selRowId = domObj.grids.$delOrderGrd.jqGrid("getGridParam","selrow");
											domObj.grids.$delOrderGrd.jqGrid("setRowData",selRowId,outObj.oary[0]);
											unableFn();
										}
									}
								}
							});
						},
						openFnc : function(){
							var selRowId, rowData, iary, inObj, outObj;
							//获得选中的行ID
							selRowId = domObj.grids.$delOrderGrd.jqGrid("getGridParam",
									"selrow");
							rowData = domObj.grids.$delOrderGrd.jqGrid(
									"getRowData", selRowId);
							if (!selRowId) {
								showErrorDialog("", "请选择需要打开的订单");
								return false;
							}
							if (rowData.dn_stat=='WAIT') {
								showErrorDialog("", "改定单已经是WAIT状态");
								return false;
							}
							//删除按钮绑定删除事件
							$("#open_btn").showCallBackWarnningDialog({
								errMsg : "是否打开订单信息,请确认!!!!",
								callbackFn : function(data) {
									if (data.result == true) {
										//获得选中行的数据
										rowData = domObj.grids.$delOrderGrd.jqGrid(
												"getRowData", selRowId);
										iary = {
                                            dn_no: rowData.dn_no,
										};
										inObj = {
											trx_id : VAL.T_FDPAPLYDN,
											action_flg : "O",
											iary : [iary]
										};
										outObj = comTrxSubSendPostJson(inObj);
										if (outObj.rtn_code === VAL.NORMAL) {
											showSuccessDialog("打开订单成功");
											selRowId = domObj.grids.$delOrderGrd.jqGrid("getGridParam","selrow");
											domObj.grids.$delOrderGrd.jqGrid("setRowData",selRowId,outObj.oary[0]);
											unableFn();
											//删除完后页面除了Parameter信息，清空
										}
									}
								}
							});
						},
						clearFnc : function(){
							domObj.$evtSeqId.val("");
							domObj.$dnno.val("");
							SelectDom.setSelect(domObj.$cusIdFk);
							SelectDom.setSelect(domObj.$mdlIdFk);
							SelectDom.setSelect(domObj.$woIdFk);
							//domObj.$plnShipQty.val("");
							domObj.$shipAddress.val("");
							domObj.$shipNote.val("");
                            domObj.$fifoFlg.attr("");
							//初始化下拉框
							init();
							$("#delOrderGrd").jqGrid('clearGridData');
							$("#mdlDefGrd").jqGrid('clearGridData');
							enableFn();
							$("#update_btn").attr(VAL.ENABLED_ATTR);
							$("#plnShipTimestamp input").val("");
						},
					}
					// 将控件变成不可修改
					function unableFn() {
                        domObj.$fifoFlg.attr(VAL.DISABLED_ATTR);
						domObj.$evtSeqId.attr(VAL.DISABLED_ATTR);
						domObj.$dnno.attr(VAL.DISABLED_ATTR);
						domObj.$cusIdFk.attr(VAL.DISABLED_ATTR);
						domObj.$mdlIdFk.attr(VAL.DISABLED_ATTR);
						//domObj.$plnShipQty.attr(VAL.DISABLED_ATTR);
						domObj.$shipAddress.attr(VAL.DISABLED_ATTR);
						$("#plnShipTimestamp input").attr(VAL.DISABLED_ATTR);
						domObj.$shipNote.attr(VAL.DISABLED_ATTR);
					}
					
					// 将控件变成都可修改
					function enableFn() {
                        domObj.$fifoFlg.attr(VAL.ENABLED_ATTR);
						domObj.$evtSeqId.attr(VAL.ENABLED_ATTR);
						domObj.$dnno.attr(VAL.ENABLED_ATTR);
						domObj.$cusIdFk.attr(VAL.ENABLED_ATTR);
						domObj.$mdlIdFk.attr(VAL.ENABLED_ATTR);
						//domObj.$plnShipQty.attr(VAL.ENABLED_ATTR);
						//domObj.$shipAddress.attr(VAL.ENABLED_ATTR);
						$("#plnShipTimestamp input").attr(VAL.ENABLED_ATTR);
						domObj.$shipNote.attr(VAL.ENABLED_ATTR);
					}

					function initMdlList(){
                        domObj.grids.$mdlDefGrd.jqGrid({
                            url : "",
                            datatype : "local",
                            mtype : "POST",
                            height : 450,
                            width : $("#mdlDefDiv").width() * 0.99,
                            autowidth : false,
                            shrinkToFit : false,
                            resizable : true,
                            loadonce : true,
                            viewrecords : true,
                            rownumbers : true,
                            rowNum : 20,
                            emptyrecords : true,
                            // multiselect : true, 多选
                            colModel : [ {
                                name : 'dn_no',
                                index : 'dn_no',
                                label : '交货订单号',
                                width : 200,
								hidden : true
                            },{
                                name : 'mdl_id_fk',
                                index : 'mdl_id_fk',
                                label : '产品代码',
                                width : 200
                            }, {
                                name : 'pln_ship_qty',
                                index : 'pln_ship_qty',
                                label : '计划交货数量',
                                width :200
                            }],
                            pager : "mdlDefPg",
                           /* onSelectRow : function(rowId) {
                                var selRowId, rowData, evtSeqId, woIdFk, inObj;
                                rowData = domObj.grids.$mdlDefGrd.jqGrid(
                                    "getRowData", rowId);
                                var dn_no = rowData.dn_no;
                               	var iary = {
                                    dn_no : dn_no,
                                }
                                inObj = {
                                    trx_id : VAL.T_FDPAPLYDN,
                                    action_flg : "Q",
                                    iary : [ iary ]
                                };
                                outObj = comTrxSubSendPostJson(inObj);
                                var oary = outObj.oary[0];
                                if (outObj.rtn_code === VAL.NORMAL) {
                                    domObj.$evtSeqId.val(oary.evt_seq_id);
                                    domObj.$dnno.val(oary.dn_no);
                                    SelectDom.setSelect(domObj.$cusIdFk,
                                        oary.cus_id_fk);
                                    SelectDom.setSelect(domObj.$mdlIdFk,
                                        oary.mdl_id_fk);
                                    domObj.$plnShipQty.val(oary.pln_ship_qty);
                                    domObj.$shipAddress.val(oary.ship_address);
                                    $("#plnShipTimestamp input").val(
                                        oary.pln_ship_timestamp);
                                    domObj.$shipNote.val(oary.ship_note);
                                    SelectDom.setSelect(domObj.$fifoFlg,
                                        oary.fifoFlg);
                                    // 不可变
                                    unableFn();
                                    $("#update_btn").attr(VAL.ENABLED_ATTR);
                                }
                            }*/
                        });

                    }
                    ;


					function initDelCusOrder() {
						domObj.grids.$delOrderGrd.jqGrid({
							url : "",
							datatype : "local",
							mtype : "POST",
							height : 500,
							width : $("#delOrderDiv").width() * 0.99,
							autowidth : false,
							shrinkToFit : false,
							resizable : true,
							loadonce : true,
							viewrecords : true,
							rownumbers : true,
							rowNum : 20,
							emptyrecords : true,
							// multiselect : true, 多选
							colModel : [  {
								name : 'cus_id_fk',
								index : 'cus_id_fk',
								label : '客户代码',
								width : 80
							},/* {
								name : 'mdl_id_fk',
								index : 'mdl_id_fk',
								label : '产品代码',
								width : 100
							}, */{
								name : 'dn_no',
								index : 'dn_no',
								label : '交货订单',
								width : 140
							}, {
								name : 'crt_timestamp',
								index : 'crt_timestamp',
								label : '创建时间',
								width : 170
							},{
								name : 'dn_stat',
								index : 'dn_stat',
								label : '状态',
								width : 100
							}, {
								name : 'pln_ship_timestamp',
								index : 'pln_ship_timestamp',
								label : '计划交货时间',
								width : 160
							},  {
								name : 'pln_ship_qtys',
								index : 'pln_ship_qtys',
								label : '总计划出货数量',
								width : 100
							},{
								name : 'act_ship_timestamp',
								index : 'act_ship_timestamp',
								label : '实际交货时间',
								width : 160
							}, {
								name : 'wo_id_fk',
								index :'wo_id_fk',
								label : '工单号',
								width : 100
							},{
								name : 'act_ship_qty',
								index : 'act_ship_qty',
								label : '已出数量',
								width : 80
							},{
								name : 'ship_note',
								index : 'ship_note',
								label : '出货备注',
								width : 80
							}],
							pager : "delOrderPg",
							onSelectRow : function(rowId) {
								var selRowId, rowData, evtSeqId, woIdFk, inObj;
								rowData = domObj.grids.$delOrderGrd.jqGrid(
										"getRowData", rowId);
								var dnNo = rowData.dn_no;
								var mdlIdFk = rowData.mdl_id_fk;
								//evtSeqId = rowData.evt_seq_id;
								var iary = {
									dn_no : dnNo,
									mdl_id_fk : mdlIdFk
								}
								inObj = {
									trx_id : VAL.T_FDPAPLYDN,
									action_flg : "Q",
									iary : [ iary ]
								};
								var outObj = comTrxSubSendPostJson(inObj);
								var oary = outObj.oary[0];
								if (outObj.rtn_code === VAL.NORMAL) {
									//domObj.$evtSeqId.val(oary.evt_seq_id);
									domObj.$dnno.val(oary.dn_no);
									SelectDom.setSelect(domObj.$cusIdFk,
											oary.cus_id_fk);
									/*SelectDom.setSelect(domObj.$mdlIdFk,
											oary.mdl_id_fk);*/
									//domObj.$plnShipQty.val(oary.pln_ship_qty);
									domObj.$shipAddress.val(oary.ship_address);
									$("#plnShipTimestamp input").val(
											oary.pln_ship_timestamp);
									domObj.$shipNote.val(oary.ship_note);
                                    SelectDom.setSelect(domObj.$fifoFlg,
                                        oary.fifoFlg);

									// 不可变
									unableFn();
									$("#update_btn").attr(VAL.ENABLED_ATTR);
								}

								var inObj2 = {
                                    trx_id : VAL.T_FDPAPLYDN,
                                    action_flg : "M",
                                    iary : [ iary ]
								}
                                var outObj2 = comTrxSubSendPostJson(inObj2);
								if(outObj2.rtn_code === VAL.NORMAL){
                                    //赋值产品代码列表
                                    setGridInfo(outObj2.oary, "#mdlDefGrd");
								}
							}
						});

					}
					;

                    var dialogFnc = {
                    		mdlAddFnc : function(){
								if(ACTION_FLG1 == 'N'){
									showErrorDialog("","请先新增或修改交货订单！");
									return false;
								}
                                $('#mdlEditDialog').modal({
                                    backdrop : true,
                                    keyboard : false,
                                    show : false
                                });
								
                                $('#mdlEditDialog').unbind('shown.bs.modal');
                                $("#mdlEditDialog_saveBtn").unbind('click');
                                $('#mdlEditDialog').bind('shown.bs.modal');
                                $('#mdlEditDialog').modal("show");
                                //模态框查询按钮绑定查询函数
                                $("#mdlEditDialog_saveBtn").bind('click', dialogFnc.addOrUpdatemdlFnc);
                                $("#mdlEditDialog input").attr(VAL.ENABLED_ATTR);
                                $("#mdlEditDialog_sel").attr(VAL.ENABLED_ATTR);
                                $('#mdlEditDialog input').val("");
							},

                        addOrUpdatemdlFnc : function () {
                            var  inObj, outObj, actionFlg,oaryB, tblCnt,newRowId,selRowId,rowData;
                            actionFlg = domObj.dialogs.$mdlEditDialog_sel.attr("disabled") === "disabled"
                                ? "U"
                                : "A";
                            var iaryB = {};
                            var  plnShipQty = domObj.dialogs.$plnShipQty.val().trim();
                            var mdlIdFk = domObj.dialogs.$mdlEditDialog_sel.val().trim();
                            if(mdlIdFk == ""){
                                showErrorDialog("", "产品代码不能为空!");
                                return false;
                            }
                            if(plnShipQty == ""){
                            	showErrorDialog("","计划交货数量不能为空！");
                            	return false;
							}

                            if(actionFlg =="A"){
                                //得到表格中所有的prdGrade，判重
                                var rowDatas = domObj.grids.$mdlDefGrd.getRowData();
                                for(var i = 0 ;i < rowDatas.length; i++){
                                    if(mdlIdFk  == rowDatas[i].mdl_id_fk){
                                        showErrorDialog("", "产品已重复请重新输入！");
                                        return false;
                                    }
                                }
                                newRowId = getGridNewRowID(domObj.grids.$mdlDefGrd);
                                domObj.grids.$mdlDefGrd.addRowData(newRowId,{'mdl_id_fk':mdlIdFk,'pln_ship_qty':plnShipQty});
                                //默认选择新加的行
                                domObj.grids.$mdlDefGrd.setSelection(newRowId, true);
                                $('#mdlEditDialog').modal("hide");
                                showSuccessDialog("新增产品成功,点击保存确认！");
                            }else if (actionFlg =="U"){
                                //更新修改行的数据
                                selRowId = domObj.grids.$mdlDefGrd.jqGrid("getGridParam","selrow");
                                domObj.grids.$mdlDefGrd.setRowData(selRowId,{'mdl_id_fk':mdlIdFk,'pln_ship_qty':plnShipQty});
                                $('#mdlEditDialog').modal("hide");
                                showSuccessDialog("修改产品成功,点击保存确认！");
                            }
                        },
	
						mdlUpdateFnc : function () {
                            if(ACTION_FLG1 == 'N'){
                                showErrorDialog("","请先新增或修改交货订单！");
                                return false;
                            }
                           /* if(domObj.$mdlDscTxt.attr("disabled") == "disabled"){
                                showErrorDialog("", "目前不是编辑状态，不可更新等级信息。");
                                return false;
                            }*/
                            var selRowId = domObj.grids.$mdlDefGrd.jqGrid("getGridParam","selrow");
                            if(selRowId == null){
                                showErrorDialog("", "请选择需要修改的产品");
                                return false;
                            }
                            var rowData = domObj.grids.$mdlDefGrd.jqGrid("getRowData",selRowId);
                            $('#mdlEditDialog').modal({
                                backdrop : true,
                                keyboard : false,
                                show : false
                            });


                            $('#mdlEditDialog').unbind('shown.bs.modal');
                            $("#mdlEditDialog_saveBtn").unbind('click');
                            $('#mdlEditDialog').bind('shown.bs.modal');
                            $('#mdlEditDialog').modal("show");
                            //模态框查询按钮绑定查询函数
                            $("#mdlEditDialog_saveBtn").bind('click', dialogFnc.addOrUpdatemdlFnc);
                            $("#mdlEditDialog input").attr(VAL.ENABLED_ATTR);
                            $('#mdlEditDialog input').val("");
                            $("#mdlEditDialog_sel").attr(VAL.DISABLED_ATTR);
                            SelectDom.setSelect(domObj.dialogs.$mdlEditDialog_sel,rowData.mdl_id_fk,rowData.mdl_id_fk);
                            $("#plnShipQty").val(rowData.pln_ship_qty);
                        },
						
						mdlDeleteFnc : function () {
                            if(ACTION_FLG1 == 'N'){
                                showErrorDialog("","请先新增或修改交货订单！");
                                return false;
                            }
                            var selRowId = domObj.grids.$mdlDefGrd.jqGrid("getGridParam","selrow");
                            if(!selRowId){
                                showErrorDialog("", "请选择需要删除的产品");
                                return false;
                            }

                            domObj.buttons.$del_mdl_btn.showCallBackWarnningDialog({
                                errMsg : "是否删除产品信息,请确认!!!!",
                                callbackFn : function(data) {
                                    if (data.result == true) {
                                        domObj.grids.$mdlDefGrd.jqGrid(
                                            "delRowData", selRowId);
                                    }
                                }
                            })
                        }

                    }

					// 初始化化下拉框函数
					var initSelectFnc = {
						// 1.初始化 客户代码
						iniCusIdSel : function(cusIdObj) {
							var inObj, outObj;
							var iary = {
								cus_typ : 'S'
							};
							inObj = {
								trx_id : VAL.T_FBPBISCUS,
								action_flg : 'Q',
								iary : [ iary ]
							};
							outObj = comTrxSubSendPostJson(inObj);
							if (outObj.rtn_code == VAL.NORMAL) {
                                SelectDom .addSelectArr(domObj.$cusIdFk, outObj.oary, "cus_id","cus_id","",true);
							}
						},
						initShipAddress : function (){
							var cus_id = domObj.$cusIdFk.val();
							var iary ={
								cus_id : cus_id
							}
							var inObj = {
                                trx_id : VAL.T_FBPBISCUS,
                                action_flg : 'Q',
                                iary : [ iary ]
							};
                            var outObj = comTrxSubSendPostJson(inObj);
                            if (outObj.rtn_code == VAL.NORMAL) {
								domObj.$shipAddress.val(outObj.oary[0].loc_info);
                            }
						},

						// 2.产品代码
						initmdlId : function() {
							var inObj, outObj, i, tblCnt, oary;
							var cusId = domObj.$cusIdFk.val();
							var iary = {
								cus_id : cusId,
								valid_flg :"Y"
							}
							inObj = {
								trx_id : VAL.T_FBPMDLDEF,
								action_flg : "Q",
								iary : [ iary ]
							};

							outObj = comTrxSubSendPostJson(inObj);
							if (outObj.rtn_code === VAL.NORMAL) {
                                SelectDom.initWithSpace(domObj.dialogs.$mdlEditDialog_sel);
								for(var i = 0 ; i < outObj.oary.length ;i++){
                                    SelectDom.addSelect(domObj.dialogs.$mdlEditDialog_sel,outObj.oary[i].mdl_id,outObj.oary[i].mdl_id);
                                    // SelectDom .addSelectArr(domObj.$mdlIdFk, outObj.oary[i], "mdl_id","mdl_id","",true);
								}
							}
						},
						
						// 3.交货日期
						iniDateTimePicker : function() {
							var datepickerData;
							domObj.$plnShipTimestamp.datetimepicker({
								language : 'zh-CN',
								autoclose : true,
								format: 'yyyy-MM-dd ',
								pickTime : false
							});
							domObj.$plnShipTimestamp.datetimepicker().on(
									'changeDate',
									function(ev) {
							domObj.$plnShipTimestamp.datetimepicker('hide');
									});
							datepickerData = domObj.$plnShipTimestamp.data("datetimepicker");

							datepickerData.setLocalDate(datepickerData.getLocalDate());
							
						},
                        // 4.先进先出
                        initfifoFlg : function() {
                            SelectDom.SelectBean(domObj.$fifoFlg, YNBean.Value, YNBean.Text, true);
                        }
					};
					function init() {
						initSelectFnc.iniCusIdSel();
						//initSelectFnc.initmdlId();
						initSelectFnc.iniDateTimePicker();
                        initSelectFnc.initfifoFlg();
                        domObj.$cusIdFk.change(initSelectFnc.initmdlId);
                        domObj.$cusIdFk.change(initSelectFnc.initShipAddress);
					}
					;
					//页面初始化
					function pageInit() {
						initDelCusOrder();
						initMdlList();
						domObj.buttons.$query_btn.bind('click',buttonFnc.queryFnc);
						domObj.buttons.$save_btn.bind('click',buttonFnc.saveFnc);
						domObj.buttons.$add_btn.bind('click',buttonFnc.addFnc);
						domObj.buttons.$update_btn.bind('click',buttonFnc.updateFnc);
						domObj.buttons.$del_btn.bind('click',buttonFnc.delFnc);
						domObj.buttons.$clear_btn.bind('click',buttonFnc.clearFnc);
						domObj.buttons.$close_btn.bind('click',buttonFnc.closeFnc);
						domObj.buttons.$open_btn.bind('click',buttonFnc.openFnc);
						domObj.buttons.$add_mdl_btn.bind('click',dialogFnc.mdlAddFnc);
						domObj.buttons.$update_mdl_btn.bind('click',dialogFnc.mdlUpdateFnc);
						domObj.buttons.$del_mdl_btn.bind('click',dialogFnc.mdlDeleteFnc);
						enableFn();
						init();
					};
					/*domObj.$cusIdFk.on("select2:select",function(e){//客户代码选定后缩小客户订单代码的范围
						   initSelectFnc.initmdlId();
						 });*/
					pageInit();
                    /**
                     * All controls's jquery object/text
                     * @type {Object}
                     */
                    var domObjs = {
                        W                   : $(window)       ,
                        grids : {
                            $UnderDiv         : $(".UnderDiv"),
                            $UnderRDiv         : $(".UnderRDiv"),
                            $delOrderDiv : $("#delOrderDiv"),
                            $delOrderGrd : $("#delOrderGrd"),
                            $mdlDefDiv : $("#mdlDefDiv"),
                            $mdlDefGrd : $("#mdlDefGrd")
                        }
                    };


                    //jqGrid表格自适应
                    function resizeFnc(){
                        var grdDivHeight,grdDivWidth,grdDivWidthR;
                        grdDivWidth = domObjs.W.width() - 75 -  domObjs.grids.$UnderRDiv.width();
                        grdDivWidthR = domObjs.W.width() - 75 -  domObjs.grids.$UnderDiv.width();
                        grdDivHeight = domObjs.W.height() - domObjs.grids.$UnderDiv.offset().top;

                        domObjs.grids.$UnderDiv.height(grdDivHeight);
                        domObjs.grids.$delOrderDiv.width(grdDivWidth);
                        domObjs.grids.$delOrderDiv.height(grdDivHeight*0.99 - 51);
                        domObjs.grids.$delOrderGrd.setGridWidth(grdDivWidth*0.99);
                        domObjs.grids.$delOrderGrd.setGridHeight(grdDivHeight*0.95 - 51 - 35);

                        domObjs.grids.$mdlDefDiv.width(grdDivWidthR);
                        domObjs.grids.$mdlDefDiv.height(grdDivHeight*0.99 - 51 - 54);
                        domObjs.grids.$mdlDefGrd.setGridWidth(grdDivWidthR*0.99);
                        domObjs.grids.$mdlDefGrd.setGridHeight(grdDivHeight*0.95 - 51 - 35 - 54);
                    };

                    resizeFnc();
                    domObjs.W.resize(function() {
                        resizeFnc();
                    });
				})